package com.jcloud.codetest1.component;

import com.jcloud.codetest1.domain.RecordItem;
import com.jcloud.codetest1.utils.MyDate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Random;

/**
 * @author
 * @date 2021-04-16 9:43
 */
@Component
public class RecordComsumer {
    private static final Logger LOG = LoggerFactory.getLogger(RecordComsumer.class);
    private  Random random = new Random();

    /**
     * 功能描述：消费数据 返回两种状态的结果 一种是已处理  一种是未知 模拟超时的情况
     * 有30%的概率返回未知  调用改方法有100毫秒的损耗
     *
     *
     * @param: [recordItem]
     * @Return: com.jcloud.codetest1.domain.RecordItem
     * @Author:  2021/4/16 10:06
     */
    public RecordItem comsumer(RecordItem recordItem){
       if(getRandomStatus().equalsIgnoreCase("Y") ){
           recordItem.setStatus("Y");
           recordItem.setDealNum(recordItem.getDealNum() + 1);
           recordItem.setDealTime(MyDate.getDateTime(new Date()));
       }else{
           recordItem.setStatus("U");
       }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
       return recordItem;
    }

    private  String getRandomStatus(){
        int ran1 = random.nextInt(100);
        if(ran1 > 70){
            return "U";
        }else {
            return "Y";
        }
    }
}
